/**
  ******************************************************************************
  * @file    STrs.c
  * @author  Hanif Rizal
  * @version V1.0.0
  * @date    03-Maret-2013
  * @brief   File ini merupakan Firmware dari Smart Transducer
  *              
  *  @verbatim
  *  
  *          ===================================================================
  *                                 How to use this driver
  *          ===================================================================              
  *   
  *  @endverbatim
  *    
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2013 Vibration Monitoring System TEAM</center></h2>
  ******************************************************************************
  */


#include <avr/io.h>
#include <util/delay.h>

#include "set_clock.h"
#include "set_adc.h"
#include "set_uart.h"
#include "set_timer.h"
#include "set_io.h"
#include "fsm_sensor.h"


void EnableAllInterrupt(void);
void DisableAllInterrupt(void);
static int uart_putchar(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE);
 
int main(void)
{
	
	Init_Clock_32MHz();
	
	myInitXMegaBoard();
	myInit_UART();
	stdout=&mystdout;
	
	//myInit_ADC();
	myEventInit_ADC();
	myTimerEventInit();
	EnableAllInterrupt();
	
	
	
	sprintf(kal,"Bismillah FSM!\r\n");
	myUART_Print(kal);
	printf(kal,"Bismillah printf FSM!\r\n");
	
    while(1)
    {
        //TODO:: Please write your application code
		fsm_sensor(&fsm_state,rx_counter0);
		
		
		if((PORTE.IN & (1<<5))==0)
		{
			while((PORTE.IN & (1<<5))==0);
			PORTR.OUTTGL=0x01;
			PORTR.OUTTGL=0x02;
			myTimerStart();
		}
		
		if((PORTF.IN & (1<<1))==0)
		{
			while((PORTF.IN & (1<<1))==0);
			PORTR.OUTTGL=0x01;
			PORTR.OUTTGL=0x02;
			myTimerStop();
		}
		
		_delay_us(80);
    }
}

ISR(USARTC0_RXC_vect)
{
	static char data=0;
	
	data = USARTC0.DATA;	//Baca Data dari USART
	rx_buffer0[rx_wr_index0++]=data;	//Simpan Data ke Buffer
	if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
	if (++rx_counter0 == RX_BUFFER_SIZE0)	//Increment Counter
	{
		rx_counter0=0;
		rx_buffer_overflow0=1;
	}
}

ISR(ADCA_CH0_vect)
{
	static int adc_val=0;
		
	adc_val = ADCA.CH0RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH1.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH1.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH1RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH2.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH2.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH2RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH3.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH3.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH3RES; //Membaca Hasil	
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	/*static int adc4,adc5,adc6,adc7;
	
	adc4 = ADCA.CH0RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc4;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH1.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH1.INTFLAGS = 1; //Clear Flag
	adc5 = ADCA.CH1RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc5;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	
	while(!ADCA.CH2.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH2.INTFLAGS = 1; //Clear Flag
	adc6 = ADCA.CH2RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc6;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH3.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH3.INTFLAGS = 1; //Clear Flag
	adc7 = ADCA.CH3RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc7;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	sprintf(kal,"adc %i %i %i %i\r",adc4,adc5,adc6,adc7);
	myUART_Print(kal);*/
}

ISR(TCD1_OVF_vect) //Interrupt vector untuk timer overflow D1
{
	timD1Cnt--;
	
	if(timD1Cnt==0)
	{
		timD1CntFlag=1;
	}
}



void EnableAllInterrupt(void)
{
	PMIC.CTRL = PMIC_LOLVLEN_bm;
	sei();
}

void DisableAllInterrupt(void)
{
	cli();
}

static int uart_putchar(char c, FILE *stream)
{
	if (c == '\n')
	uart_putchar('\r', stream);
	while ( (USARTC0.STATUS & USART_DREIF_bm) == 0); 
	USARTC0.DATA = c;
	return 0;
}
